package com.wythe.mall.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wythe.mall.R;
import com.wythe.mall.activity.GoodsActivity;
import com.wythe.mall.model.ShopBean;
import com.wythe.mall.utils.DPIUtil;
import com.wythe.mall.utils.DensityUtils;
import com.wythe.mall.utils.GotoActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 图片适配器
 * Created by Yancy on 2015/12/2.
 */
public class ImageShopAdapter extends BaseAdapter {

    private Context context;
    private LayoutInflater mLayoutInflater;
    private List<ShopBean> imageList;
    private final static String TAG = "ImageAdapter";

    private static final int TYPE_CAMERA = 0;
    private static final int TYPE_NORMAL = 1;

    private boolean showCamera = true;
    private int mItemSize;
    private int screenWidth;
    public ImageShopAdapter(Context context, List<ShopBean> imageList) {
        mLayoutInflater = LayoutInflater.from(context);
        this.context = context;
        this.imageList = imageList;
        screenWidth = DensityUtils.screenWidth(context);
    }

    @Override
    public int getCount() {
        return imageList.size();
    }

    @Override
    public Object getItem(int position) {
        if (showCamera) {
            if (position == 0) {
                return null;
            }
            return imageList.get(position - 1);
        } else {
            return imageList.get(position);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ShopBean shopBean = imageList.get(position);
        ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.item_home_shop_h, parent, false);
            holder = new ViewHolder(convertView);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        GridView.LayoutParams layoutParams = (GridView.LayoutParams) convertView.getLayoutParams();
        layoutParams.width = (int) ((screenWidth - DPIUtil.dip2px(context,45)) / 2);
        convertView.setLayoutParams(layoutParams);
        LinearLayout.LayoutParams layoutParams1 = (LinearLayout.LayoutParams) holder.photo_image.getLayoutParams();
        layoutParams1.height = DPIUtil.dip2px(context,120);
        holder.photo_image.setLayoutParams(layoutParams1);
        Glide.with(context).load(shopBean.getIcon()).error(R.drawable.personal_icon_default_avatar).into(holder.photo_image);
        holder.tvPrice.setText("￥" + shopBean.getSkuVos().get(0).getPrice());
        holder.tvOrginPrice.setText("￥" + shopBean.getSkuVos().get(0).getOriginalPrice());
        holder.tvOrginPrice.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG);
        holder.tvDes.setText(shopBean.getTitle());
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putInt("id",shopBean.getId());
                GotoActivity.gotoActiviy((Activity) context, GoodsActivity.class,bundle);
            }
        });

        return convertView;
    }

    class ViewHolder {
        ImageView photo_image;
        TextView tvDes;
        TextView tvPrice;
        TextView tvOrginPrice;

        ViewHolder(View itemView) {
            photo_image = (ImageView) itemView.findViewById(R.id.iv_shop);
            tvDes = (TextView) itemView.findViewById(R.id.tv_des);
            tvPrice = (TextView) itemView.findViewById(R.id.tv_price);
            tvOrginPrice = itemView.findViewById(R.id.tv_orgin_price);
            itemView.setTag(this);
        }

    }


    @Override
    public int getItemViewType(int position) {
//        if (showCamera && position == 0) {
////            return TYPE_CAMERA;
////        }
        return TYPE_NORMAL;
    }
}